package com.zsummer.baikal.common.web;

import com.zsummer.baikal.common.constant.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author zsummer
 * @since 2021/7/31
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        log.error(e.getMessage(), e);
        return R.failed(e.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public Object handleAccessDeniedException(Exception e) {
        log.error(e.getMessage(), e);
        log.error(SecurityContextHolder.getContext().getAuthentication().getAuthorities().toString());
        return R.restResult(null, HttpStatus.FORBIDDEN, "没有权限访问该资源");
    }

}
